package cn.yunyichina.provider.sb.service.impl;

import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.framework.dubbo.service.HisifaceService;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.hisiface.shangbao.*;
import cn.yunyichina.provider.sb.constant.Constant;
import cn.yunyichina.provider.sb.dao.*;
import cn.yunyichina.provider.sb.entity.*;
import cn.yunyichina.provider.sb.util.Util;
import cn.yunyichina.provider.sb.vo.MedicalOutpatientFeeDetailVo;
import cn.yunyichina.provider.sb.vo.MedicalOutpatientFeeVo;
import cn.yunyichina.provider.sb.vo.SbRequest;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alipay.api.response.AlipayCommerceDataSendResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 15-AK on 2017/7/22.
 */
public class MedicalFeeServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(MedicalFeeServiceImpl.class);
    private MedicalOutpatientFeeDao medicalOutpatientFeeDao = SpringContextHandler.getBean("medicalOutpatientFeeDao");
    private MedicalOutpatientFeeDetailDao medicalOutpatientFeeDetailDao = SpringContextHandler.getBean("medicalOutpatientFeeDetailDao");
    private MedicalPatientDao medicalPatientDao = SpringContextHandler.getBean("medicalPatientDao");
    private MedicalCheckingDao medicalCheckingDao = SpringContextHandler.getBean("medicalCheckingDao");
    private MedicalCheckingDetailDao medicalCheckingDetailDao = SpringContextHandler.getBean("medicalCheckingDetailDao");
    private MedicalExaminationDao medicalExaminationDao = SpringContextHandler.getBean("medicalExaminationDao");
    private MedicalOperationRecordDao medicalOperationRecordDao = SpringContextHandler.getBean("medicalOperationRecordDao");
    private final static String HOSPITAL_CODE = "szszyy";

    public void getMedicalFeeRecords() {
        try {
            List<MedicalPatient> notDownLoadMedicalFeeInfo = medicalPatientDao.selectNotDownLoadMedicalFeeInfo(HOSPITAL_CODE);
            if (CollectionUtils.isNotEmpty(notDownLoadMedicalFeeInfo)) {
                for (MedicalPatient medicalPatientExtendVo : notDownLoadMedicalFeeInfo) {
                    SBMZFeeRecordsVo feeRecordsVo = new SBMZFeeRecordsVo();
                    feeRecordsVo.setMzFeeId(medicalPatientExtendVo.getMzFeeId());
                    feeRecordsVo.setHospitalCode(medicalPatientExtendVo.getOrgCode());
                    feeRecordsVo.setDeptCode(medicalPatientExtendVo.getVisitDeptCode());
                    feeRecordsVo.setDoctorCode(medicalPatientExtendVo.getDiagnosisDoctorCode());
                    logger.info("mzFeeId={},获取商保门诊费用信息his入参feeRecordsVo ={}", new Object[]{medicalPatientExtendVo.getMzFeeId(),JsonUtils.toJsonString(feeRecordsVo)});
                    ResCommon<List<SBMZFeeRecordsVoResponse>> hisfiaceMzFeeRecordsResponse = HisifaceService.getHisifaceClient().getSBMZFeeRecords(feeRecordsVo);
                    logger.info("mzFeeId={},获取商保门诊费用信息his出参hisfiaceMzFeeRecordsResponse ={}", new Object[]{medicalPatientExtendVo.getMzFeeId(),JsonUtils.toJsonString(hisfiaceMzFeeRecordsResponse)});
                    List<SBMZFeeRecordsVoResponse> hisSBMZFeeRecordsVoResponseList = hisfiaceMzFeeRecordsResponse.getResult();
                    if (!"0".equals(hisfiaceMzFeeRecordsResponse.getResultCode())) {
                        medicalPatientExtendVo.setDownLoadFeeExceptionMessage(hisfiaceMzFeeRecordsResponse.getResultMessage());
                        medicalPatientExtendVo.setDownLoadFeeTimes(medicalPatientExtendVo.getDownLoadFeeTimes() + 1);
                        medicalPatientDao.update(medicalPatientExtendVo);
                        continue;
                    }

                    if (CollectionUtils.isNotEmpty(hisSBMZFeeRecordsVoResponseList)) {
                        List<MedicalOutpatientFee> medicalOutpatientFeeList = new ArrayList<MedicalOutpatientFee>();
                        List<MedicalOutpatientFeeDetail> medicalOutpatientFeeDetailList = new ArrayList<>();
                        int feeOffSet = 1;
                        for (SBMZFeeRecordsVoResponse sBMZFeeRecordsVoResponse : hisSBMZFeeRecordsVoResponseList) {
                            MedicalOutpatientFee medicalOutpatientFee = buildMedicalOutPatientFee(sBMZFeeRecordsVoResponse);
                            medicalOutpatientFee.setCaseNo(medicalPatientExtendVo.getCaseNo());
                            medicalOutpatientFee.setMedicalPatientId(medicalPatientExtendVo.getId());
                            medicalOutpatientFee.setMzFeeId(medicalPatientExtendVo.getMzFeeId());
                            medicalOutpatientFee.setCurrentOffset(feeOffSet);
                            medicalOutpatientFee.setTotalRows(hisSBMZFeeRecordsVoResponseList.size());
                            medicalOutpatientFee.setUploadTimes(0);
                            medicalOutpatientFee.setUploadFlag(new Short("0"));
                            medicalOutpatientFeeList.add(medicalOutpatientFee);

                            List<SBMZFeeDetail> subItems = sBMZFeeRecordsVoResponse.getSubItems();
                            if (CollectionUtils.isNotEmpty(subItems)) {
                                for (SBMZFeeDetail sBMZFeeDetail : subItems) {
                                    MedicalOutpatientFeeDetail medicalOutpatientFeeDetail = buildMedicalOutPatientFeeDetail(sBMZFeeDetail);
                                    medicalOutpatientFeeDetail.setCaseNo(medicalPatientExtendVo.getCaseNo());
                                    medicalOutpatientFeeDetail.setPayNo(medicalOutpatientFee.getPayNo());
                                    medicalOutpatientFeeDetail.setMedicalPatientId(medicalPatientExtendVo.getId());
                                    medicalOutpatientFeeDetail.setMedicalOutpatientFeeId(medicalOutpatientFee.getId());
                                    medicalOutpatientFeeDetail.setMzFeeId(medicalPatientExtendVo.getMzFeeId());
                                    medicalOutpatientFeeDetail.setUploadTimes(0);
                                    medicalOutpatientFeeDetail.setUploadFlag(new Short("0"));
                                    medicalOutpatientFeeDetail.setDownLoadReportFlag(new Short("0"));
                                    medicalOutpatientFeeDetail.setDownLoadReportTimes(0);
                                    medicalOutpatientFeeDetailList.add(medicalOutpatientFeeDetail);
                                }
                            }
                            feeOffSet++;
                        }

                        int feeDetailOffSet = 1;
                        for (MedicalOutpatientFeeDetail medicalOutpatientFeeDetail : medicalOutpatientFeeDetailList) {
                            medicalOutpatientFeeDetail.setCurrentOffset(feeDetailOffSet++);
                            medicalOutpatientFeeDetail.setTotalRows(medicalOutpatientFeeDetailList.size());
                            //去调用his的检验，检查，手术记录接口
                            if (medicalOutpatientFeeDetail.getFeeType() != null && medicalOutpatientFeeDetail.getFeeType() == 9 && StringUtils.isNotEmpty(medicalOutpatientFeeDetail.getReportId())) {
                                MedicalChecking queryMedicalChecking = new MedicalChecking();
                                queryMedicalChecking.setOrgCode(medicalOutpatientFeeDetail.getOrgCode());
                                queryMedicalChecking.setInspectId(medicalOutpatientFeeDetail.getReportId());
                                List<MedicalChecking> medicalCheckingList = medicalCheckingDao.select(queryMedicalChecking);
                                //假如检验his已经回调已出通知了，那么就将检验跟就诊人关联起来，否者要主动去拉his的检验
                                if (CollectionUtils.isNotEmpty(medicalCheckingList)) {
                                    //更新费用明细状态
                                    medicalOutpatientFeeDetail.setNotifyFlag(new Short("1"));
                                    medicalOutpatientFeeDetail.setDownLoadReportFlag(new Short("1"));
                                    medicalOutpatientFeeDetail.setDownLoadReportTimes(1);
                                    //将检验跟费用明细关联
                                    medicalCheckingList.get(0).setCaseNo(medicalOutpatientFeeDetail.getCaseNo());
                                    medicalCheckingList.get(0).setMedicalPatientId(medicalOutpatientFeeDetail.getMedicalPatientId());
                                    medicalCheckingList.get(0).setMedicalOutpatientFeeDetailId(medicalOutpatientFeeDetail.getId());
                                    medicalCheckingDao.update(medicalCheckingList.get(0));
                                    //更新检验明细
                                    MedicalCheckingDetail queryMedicalCheckingDetail = new MedicalCheckingDetail();
                                    queryMedicalCheckingDetail.setMedicalCheckingId(medicalCheckingList.get(0).getId());
                                    List<MedicalCheckingDetail> updateMedicalCheckingDetail = medicalCheckingDetailDao.select(queryMedicalCheckingDetail);
                                    for (MedicalCheckingDetail medicalCheckingDetail : updateMedicalCheckingDetail) {
                                        medicalCheckingDetail.setMedicalCheckingId(medicalCheckingList.get(0).getId());
                                        medicalCheckingDetail.setMedicalPatientId(medicalCheckingList.get(0).getMedicalPatientId());
                                        medicalCheckingDetail.setCaseNo(medicalCheckingList.get(0).getCaseNo());
                                        medicalCheckingDetailDao.update(medicalCheckingDetail);
                                    }

                                } else {

                                    //主动拉his的检验
                                    SBInspectRecordVo inspectRecordVo = new SBInspectRecordVo();
                                    inspectRecordVo.setHospitalCode(medicalOutpatientFeeDetail.getOrgCode());
                                    inspectRecordVo.setInspectId(medicalOutpatientFeeDetail.getReportId());
                                    //拿到检验
                                    logger.info("获取商保检验信息his入参 inspectRecordVo ={}", new Object[]{JsonUtils.toJsonString(inspectRecordVo)});
                                    ResCommon<SBInspectRecordVoResponse> hisfiaceSBInspectRecordResponse = HisifaceService.getHisifaceClient().getSBInspectRecord(inspectRecordVo);
                                    logger.info("获取商保检验信息his出参 hisfiaceSBInspectRecordResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceSBInspectRecordResponse)});
                                    if (!"0".equals(hisfiaceSBInspectRecordResponse.getResultCode())) {
                                        continue;
                                    }

                                    SBInspectRecordVoResponse sBInspectRecordVoResponse = hisfiaceSBInspectRecordResponse.getResult();
                                    if (sBInspectRecordVoResponse == null) {
                                        continue;
                                    }
                                    //通过reportId 查找对应的检验
                                    MedicalChecking medicalChecking = null;
                                    List<MedicalCheckingDetail> medicalCheckingDetails = null;

                                    medicalChecking = buildMedicalChecking(sBInspectRecordVoResponse);
                                    //患者信息id
                                    List<SBInspectDetail> inspectDetails = sBInspectRecordVoResponse.getInspectDetails();
                                    if (CollectionUtils.isNotEmpty(inspectDetails)) {
                                        medicalCheckingDetails = new ArrayList<MedicalCheckingDetail>();
                                        for (SBInspectDetail sBInspectDetail : inspectDetails) {
                                            MedicalCheckingDetail medicalCheckingDetail = buildMedicalCheckingDetail(sBInspectDetail);
                                            medicalCheckingDetail.setBusinessFeeId(medicalChecking.getBusinessFeeId());
                                            medicalCheckingDetail.setInspectId(medicalChecking.getInspectId());
                                            medicalCheckingDetail.setBusinessType(convertStringToShort(sBInspectRecordVoResponse.getBusinessType()));
                                            medicalCheckingDetail.setOrgCode(medicalChecking.getOrgCode());
                                            medicalCheckingDetail.setMedicalCheckingId(medicalChecking.getId());
                                            medicalCheckingDetails.add(medicalCheckingDetail);
                                        }
                                    }


                                    medicalChecking.setMedicalPatientId(medicalOutpatientFeeDetail.getMedicalPatientId());
                                    medicalChecking.setMedicalOutpatientFeeDetailId(medicalOutpatientFeeDetail.getId());
                                    medicalChecking.setCaseNo(medicalOutpatientFeeDetail.getCaseNo());
                                    for (MedicalCheckingDetail medicalCheckingDetail : medicalCheckingDetails) {
                                        medicalCheckingDetail.setMedicalPatientId(medicalOutpatientFeeDetail.getMedicalPatientId());
                                        medicalCheckingDetail.setCaseNo(medicalOutpatientFeeDetail.getCaseNo());
                                    }

                                    medicalOutpatientFeeDetail.setDownLoadReportFlag(new Short("1"));
                                    medicalOutpatientFeeDetail.setNotifyFlag(new Short("1"));
                                    medicalOutpatientFeeDetail.setDownLoadReportTimes(medicalOutpatientFeeDetail.getDownLoadReportTimes() + 1);
                                    medicalCheckingDao.insert(medicalChecking);
                                    if(CollectionUtils.isNotEmpty(medicalCheckingDetails)){
                                        medicalCheckingDetailDao.batchInsert(medicalCheckingDetails);
                                    }
                                }
                            }

                            //检查
                            if (medicalOutpatientFeeDetail.getFeeType() != null && medicalOutpatientFeeDetail.getFeeType() == 4 && StringUtils.isNotEmpty(medicalOutpatientFeeDetail.getReportId())) {
                                MedicalExamination queryMedicalExamination = new MedicalExamination();
                                queryMedicalExamination.setCheckId(medicalOutpatientFeeDetail.getReportId());
                                queryMedicalExamination.setOrgCode(medicalOutpatientFeeDetail.getOrgCode());
                                List<MedicalExamination> medicalExaminationList = medicalExaminationDao.select(queryMedicalExamination);
                                if (CollectionUtils.isNotEmpty(medicalExaminationList)) {
                                    //更新费用明细状态
                                    medicalOutpatientFeeDetail.setNotifyFlag(new Short("1"));
                                    medicalOutpatientFeeDetail.setDownLoadReportFlag(new Short("1"));
                                    medicalOutpatientFeeDetail.setDownLoadReportTimes(1);
                                    //将手术记录跟费用明细关联
                                    medicalExaminationList.get(0).setCaseNo(medicalOutpatientFeeDetail.getCaseNo());
                                    medicalExaminationList.get(0).setMedicalPatientId(medicalOutpatientFeeDetail.getMedicalPatientId());
                                    medicalExaminationList.get(0).setMedicalOutpatientFeeDetailId(medicalOutpatientFeeDetail.getId());
                                    medicalExaminationDao.update(medicalExaminationList.get(0));
                                } else {

                                    //拿检查
                                    SBCheckRecordVo checkRecordVo = new SBCheckRecordVo();
                                    checkRecordVo.setHospitalCode(medicalOutpatientFeeDetail.getOrgCode());
                                    checkRecordVo.setCheckId(medicalOutpatientFeeDetail.getReportId());
                                    logger.info("获取门诊商保检查信息his入参checkRecordVo ={}", new Object[]{JsonUtils.toJsonString(checkRecordVo)});
                                    ResCommon<SBCheckRecordVoResponse> hisfiaceSBCheckRecordResponse = HisifaceService.getHisifaceClient().getSBCheckRecord(checkRecordVo);
                                    logger.info("获取门诊商保检查信息his出参 hisfiaceSBCheckRecordResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceSBCheckRecordResponse)});

                                    if (!"0".equals(hisfiaceSBCheckRecordResponse.getResultCode())) {
                                        continue;
                                    }
                                    SBCheckRecordVoResponse sbCheckRecordVoResponse = hisfiaceSBCheckRecordResponse.getResult();
                                    if (sbCheckRecordVoResponse == null) {
                                        continue;
                                    }

                                    MedicalExamination examination = buildExamination(sbCheckRecordVoResponse);
                                    //费用已经拉取的话就把检查跟费用明细关联
                                    if (examination != null) {
                                        medicalOutpatientFeeDetail.setDownLoadReportFlag(new Short("1"));
                                        medicalOutpatientFeeDetail.setNotifyFlag(new Short("1"));
                                        medicalOutpatientFeeDetail.setDownLoadReportTimes(medicalOutpatientFeeDetail.getDownLoadReportTimes() + 1);

                                        examination.setOrgCode(medicalOutpatientFeeDetail.getOrgCode());
                                        examination.setMedicalPatientId(medicalOutpatientFeeDetail.getMedicalPatientId());
                                        examination.setMedicalOutpatientFeeDetailId(medicalOutpatientFeeDetail.getId());
                                        examination.setCaseNo(medicalOutpatientFeeDetail.getCaseNo());
                                    }

                                    medicalExaminationDao.insert(examination);
                                }
                            }

                            //手术记录
                            if (medicalOutpatientFeeDetail.getFeeType() != null && medicalOutpatientFeeDetail.getFeeType() == 10 && StringUtils.isNotEmpty(medicalOutpatientFeeDetail.getReportId())) {
                                MedicalOperationRecord queryMedicalOperationRecord = new MedicalOperationRecord();
                                queryMedicalOperationRecord.setSurgeryId(medicalOutpatientFeeDetail.getReportId());
                                queryMedicalOperationRecord.setOrgCode(medicalOutpatientFeeDetail.getOrgCode());
                                List<MedicalOperationRecord> medicalOperationRecordList = medicalOperationRecordDao.select(queryMedicalOperationRecord);
                                if (CollectionUtils.isNotEmpty(medicalOperationRecordList)) {
                                    //更新费用明细状态
                                    medicalOutpatientFeeDetail.setNotifyFlag(new Short("1"));
                                    medicalOutpatientFeeDetail.setDownLoadReportFlag(new Short("1"));
                                    medicalOutpatientFeeDetail.setDownLoadReportTimes(1);
                                    //将手术记录跟费用明细关联
                                    medicalOperationRecordList.get(0).setCaseNo(medicalOutpatientFeeDetail.getCaseNo());
                                    medicalOperationRecordList.get(0).setMedicalPatientId(medicalOutpatientFeeDetail.getMedicalPatientId());
                                    medicalOperationRecordList.get(0).setMedicalOutpatientFeeDetailId(medicalOutpatientFeeDetail.getId());
                                    medicalOperationRecordDao.update(medicalOperationRecordList.get(0));
                                }
                            }
                        }
                        medicalOutpatientFeeDetailDao.batchInsert(medicalOutpatientFeeDetailList);
                        medicalOutpatientFeeDao.batchInsert(medicalOutpatientFeeList);
                        medicalPatientExtendVo.setDownLoadFeeFlag(new Short("1"));
                        medicalPatientExtendVo.setDownLoadFeeTimes(medicalPatientExtendVo.getDownLoadFeeTimes() + 1);
                        medicalPatientDao.update(medicalPatientExtendVo);
                    }
                }
            }

        } catch (Exception e) {
            logger.info("获取商保门诊费用信息异常", e);
        }
    }


    public void executeUploadMedicalOutPatientFeeMessage() {
        try {
            List<MedicalOutpatientFee> unuploadMedicalOutpatientFeeList = medicalOutpatientFeeDao.selectUnuploadMedicalOutpatientFee(HOSPITAL_CODE);
            for (MedicalOutpatientFee unuploadMedicalOutpatientFee : unuploadMedicalOutpatientFeeList) {
                logger.info("待上传的门诊费用列表信息unuploadMedicalOutpatientFee ={}", new Object[]{JsonUtils.toJsonString(unuploadMedicalOutpatientFee)});
                MedicalOutpatientFeeVo medicalOutpatientFeeVo = new MedicalOutpatientFeeVo();
                String encryptData = Util.encryptMedicalOutpatientFeeVo(medicalOutpatientFeeVo.buildMedicalOutpatientFeeVo(unuploadMedicalOutpatientFee), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(Constant.MEDICAL_OUTPATIENT_FEE);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("门诊费用列表信息上传支付宝响应参数 data ={}", new Object[]{JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("门诊费用列表信息上传成功");
                        unuploadMedicalOutpatientFee.setUploadFlag(new Short("1"));
                        unuploadMedicalOutpatientFee.setUploadTimes(unuploadMedicalOutpatientFee.getUploadTimes() + 1);
                    } else {
                        logger.info("门诊费用列表信息上传失败");
                        unuploadMedicalOutpatientFee.setExceptionMessage(response.getSubMsg());
                        unuploadMedicalOutpatientFee.setUploadTimes(unuploadMedicalOutpatientFee.getUploadTimes() + 1);
                    }
                    medicalOutpatientFeeDao.update(unuploadMedicalOutpatientFee);
                } catch (Exception e) {
                    logger.error("门诊费用列表信息上传系统异常", e);
                }
            }

        } catch (Exception e) {
            logger.error("门诊费用列表信息上传系统异常", e);
        }
    }


    public void executeUploadMedicalOutPatientFeeDetailMessage() {
        try {
            List<MedicalOutpatientFeeDetail> selectUnuploadMedicalOutpatientFeeDetailList = medicalOutpatientFeeDetailDao.selectUnuploadMedicalOutpatientFeeDetail(HOSPITAL_CODE);
            for (MedicalOutpatientFeeDetail medicalOutpatientFeeDetail : selectUnuploadMedicalOutpatientFeeDetailList) {
                logger.info("待上传的门诊费用明细信息 medicalOutpatientFeeDetail ={}", new Object[]{JsonUtils.toJsonString(medicalOutpatientFeeDetail)});
                MedicalOutpatientFeeDetailVo medicalOutpatientFeeDetailVo = new MedicalOutpatientFeeDetailVo();
                String encryptData = Util.encryptMedicalOutpatientFeeDetailVo(medicalOutpatientFeeDetailVo.buildMedicalOutpatientFeeDetailVo(medicalOutpatientFeeDetail), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(Constant.MEDICAL_OUTPATIENT_FEE_DETAIL);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("门诊费用明细信息上传支付宝响应参数 data ={}", new Object[]{JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("门诊费用明细信息上传成功");
                        medicalOutpatientFeeDetail.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("门诊费用明细信息上传失败");
                        medicalOutpatientFeeDetail.setExceptionMessage(response.getSubMsg());
                    }
                    medicalOutpatientFeeDetail.setUploadTimes(medicalOutpatientFeeDetail.getUploadTimes() + 1);
                    medicalOutpatientFeeDetailDao.update(medicalOutpatientFeeDetail);
                } catch (Exception e) {
                    logger.error("门诊费用明细信息上传系统异常", e);
                }
            }
        } catch (Exception e) {
            logger.error("门诊费用明细信息上传系统异常", e);
        }
    }


    private MedicalOutpatientFeeDetail buildMedicalOutPatientFeeDetail(SBMZFeeDetail sBMZFeeDetail) {
        MedicalOutpatientFeeDetail medicalOutpatientFeeDetail = new MedicalOutpatientFeeDetail();
        medicalOutpatientFeeDetail.setId(PKGenerator.generateId());
        medicalOutpatientFeeDetail.setMzFeeDetailId(sBMZFeeDetail.getMzFeeDetailId());
        medicalOutpatientFeeDetail.setReportId(sBMZFeeDetail.getReportId());
        medicalOutpatientFeeDetail.setFeeType(new Short(sBMZFeeDetail.getFeeType()));
        medicalOutpatientFeeDetail.setFeeItemName(sBMZFeeDetail.getFeeItemName());
        medicalOutpatientFeeDetail.setFeeItemCode(sBMZFeeDetail.getFeeItemCode());
        medicalOutpatientFeeDetail.setFeeItemUnit(sBMZFeeDetail.getFeeItemUnit());
        medicalOutpatientFeeDetail.setFeeItemSpec(sBMZFeeDetail.getFeeItemSpec());
        medicalOutpatientFeeDetail.setFeeItemPrice(conveterStringToInt(sBMZFeeDetail.getFeeItemPrice()));
        medicalOutpatientFeeDetail.setFeeItemQty(sBMZFeeDetail.getFeeItemQty());
        medicalOutpatientFeeDetail.setFeeItemAmount(conveterStringToInt(sBMZFeeDetail.getFeeItemAmount()));
        medicalOutpatientFeeDetail.setTotalAmount(conveterStringToInt(sBMZFeeDetail.getTotalAmount()));
        medicalOutpatientFeeDetail.setSelfBurdenPercentage(sBMZFeeDetail.getSelfBurdenPercentage() == null ? null : Double.valueOf(sBMZFeeDetail.getSelfBurdenPercentage()));
        medicalOutpatientFeeDetail.setItemSpec(sBMZFeeDetail.getItemSpec());
        medicalOutpatientFeeDetail.setItemType(sBMZFeeDetail.getItemType());
        medicalOutpatientFeeDetail.setItemUnit(sBMZFeeDetail.getItemUnit());
        medicalOutpatientFeeDetail.setItemOrigin(sBMZFeeDetail.getItemOrigin());
        medicalOutpatientFeeDetail.setPackageUnit(sBMZFeeDetail.getPackageUnit());
        medicalOutpatientFeeDetail.setMiniDosageUnit(sBMZFeeDetail.getMiniDosageUnit());
        medicalOutpatientFeeDetail.setPerDose(sBMZFeeDetail.getPerDose());
        medicalOutpatientFeeDetail.setDosageUnit(sBMZFeeDetail.getDosageUnit());
        medicalOutpatientFeeDetail.setPerDayTimes(sBMZFeeDetail.getPerDayTimes());
        medicalOutpatientFeeDetail.setMedicineDays(sBMZFeeDetail.getMedicineDays());
        medicalOutpatientFeeDetail.setSingleCompoundFlag(sBMZFeeDetail.getSingleCompoundFlag());
        medicalOutpatientFeeDetail.setChineseHerbalMedicineQty(sBMZFeeDetail.getChineseHerbalMedicineQty());
        medicalOutpatientFeeDetail.setItemLimitPrice(conveterStringToInt(sBMZFeeDetail.getItemLimitPrice()));
        medicalOutpatientFeeDetail.setApprovalNo(sBMZFeeDetail.getApprovalNo());
        medicalOutpatientFeeDetail.setSelfFeeFlag(sBMZFeeDetail.getSelfFeeFlag());
        medicalOutpatientFeeDetail.setSpecialMedicineFlag(sBMZFeeDetail.getSpecialMedicineFlag());
        medicalOutpatientFeeDetail.setInsuranceAuxiliaryInfo(sBMZFeeDetail.getInsuranceAuxiliaryInfo());
        medicalOutpatientFeeDetail.setSingleQty(sBMZFeeDetail.getSingleQty());
        medicalOutpatientFeeDetail.setFrequencyValue(sBMZFeeDetail.getFrequencyValue());
        medicalOutpatientFeeDetail.setMedicineMethod(sBMZFeeDetail.getMedicineMethod());
        medicalOutpatientFeeDetail.setFrequencyCode(sBMZFeeDetail.getFrequencyCode());
        medicalOutpatientFeeDetail.setItemCode(sBMZFeeDetail.getItemCode());
        medicalOutpatientFeeDetail.setDrugSn(sBMZFeeDetail.getDrugSn());
        medicalOutpatientFeeDetail.setDrugOrigin(sBMZFeeDetail.getDrugOrigin());
        medicalOutpatientFeeDetail.setDrugSpecQty(sBMZFeeDetail.getDrugSpecQty());
        medicalOutpatientFeeDetail.setItemExecDate(sBMZFeeDetail.getItemExecDate());
        medicalOutpatientFeeDetail.setDoctorCode(sBMZFeeDetail.getDoctorCode());
        medicalOutpatientFeeDetail.setDoctorName(sBMZFeeDetail.getDoctorName());
        medicalOutpatientFeeDetail.setCombination(sBMZFeeDetail.getCombination());
        medicalOutpatientFeeDetail.setFunctions(sBMZFeeDetail.getFunctions());
        medicalOutpatientFeeDetail.setContraindication(sBMZFeeDetail.getContraindication());
        medicalOutpatientFeeDetail.setRecipeFlag(sBMZFeeDetail.getRecipeFlag());
        medicalOutpatientFeeDetail.setRestrictionsFlag(sBMZFeeDetail.getRestrictionsFlag());
        medicalOutpatientFeeDetail.setRestrictions(sBMZFeeDetail.getRestrictions());
        medicalOutpatientFeeDetail.setBaseDrugIdentification(sBMZFeeDetail.getBaseDrugIdentification());
        medicalOutpatientFeeDetail.setNationalMedicineId(sBMZFeeDetail.getNationalMedicineId());
        medicalOutpatientFeeDetail.setRegId(sBMZFeeDetail.getRegId());
        medicalOutpatientFeeDetail.setBrand(sBMZFeeDetail.getBrand());
        medicalOutpatientFeeDetail.setItemEnglishName(sBMZFeeDetail.getItemEnglishName());
        medicalOutpatientFeeDetail.setInsuranceRange(sBMZFeeDetail.getInsuranceRange());
        medicalOutpatientFeeDetail.setMaxLimitPrice1(conveterStringToInt(sBMZFeeDetail.getMaxLimitPrice1()));
        medicalOutpatientFeeDetail.setMaxLimitPrice2(conveterStringToInt(sBMZFeeDetail.getMaxLimitPrice2()));
        medicalOutpatientFeeDetail.setMaxLimitPrice3(conveterStringToInt(sBMZFeeDetail.getMaxLimitPrice3()));
        medicalOutpatientFeeDetail.setMaxLimitPrice4(conveterStringToInt(sBMZFeeDetail.getMaxLimitPrice4()));
        medicalOutpatientFeeDetail.setInsuranceSettleRangeAmount(conveterStringToInt(sBMZFeeDetail.getInsuranceSettleRangeAmount()));
        medicalOutpatientFeeDetail.setManufacturer(sBMZFeeDetail.getManufacturer());
        medicalOutpatientFeeDetail.setOrgCode(HOSPITAL_CODE);
        medicalOutpatientFeeDetail.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalOutpatientFeeDetail;
    }


    private MedicalOutpatientFee buildMedicalOutPatientFee(SBMZFeeRecordsVoResponse sBMZFeeRecordsVoResponse) {
        MedicalOutpatientFee medicalOutpatientFee = new MedicalOutpatientFee();
        medicalOutpatientFee.setId(PKGenerator.generateId());
        medicalOutpatientFee.setMzFeeId(sBMZFeeRecordsVoResponse.getMzFeeId());
        medicalOutpatientFee.setReFundFeeId(sBMZFeeRecordsVoResponse.getRefundFeeId());
        medicalOutpatientFee.setSocialSecurityNo(sBMZFeeRecordsVoResponse.getSocialSecurityNo());
        medicalOutpatientFee.setInsuranceType(sBMZFeeRecordsVoResponse.getInsuranceType());
        medicalOutpatientFee.setInvoiceNo(sBMZFeeRecordsVoResponse.getInvoiceNo());
        medicalOutpatientFee.setListType(convertStringToShort(sBMZFeeRecordsVoResponse.getListType()));
        medicalOutpatientFee.setMedicalFeeSettleCode(sBMZFeeRecordsVoResponse.getMedicalFeeSettleCode());
        medicalOutpatientFee.setInvoiceTotalAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getInvoiceTotalAmount()));
        medicalOutpatientFee.setSelfAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getSelfAmount()));
        medicalOutpatientFee.setLargeAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getLargeAmount()));
        medicalOutpatientFee.setFundAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getFundAmount()));
        medicalOutpatientFee.setCurYearPayAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getCurYearPayAmount()));
        medicalOutpatientFee.setCivilServantSubsidyAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getCivilServantSubsidyAmount()));
        medicalOutpatientFee.setPerBurdenAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getPerBurdenAmount()));
        medicalOutpatientFee.setCashAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getCashAmount()));
        medicalOutpatientFee.setCurYearBalance(conveterStringToInt(sBMZFeeRecordsVoResponse.getCurYearBalance()));
        medicalOutpatientFee.setPastYearPayAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getPastYearPayAmount()));
        medicalOutpatientFee.setSeriousIllnessComplianceAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getSeriousIllnessComplianceAmount()));
        medicalOutpatientFee.setRescueFundAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getLamorFundAmount()));
        medicalOutpatientFee.setHealthFundAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getHealthFundAmount()));
        medicalOutpatientFee.setPromptMessage(sBMZFeeRecordsVoResponse.getPromptMessage());
        medicalOutpatientFee.setOldWorkerFundAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getOldWorkerFundAmount()));
        medicalOutpatientFee.setHospitalCostAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getHospitalCostAmount()));
        medicalOutpatientFee.setOtherFundAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getOtherFundAmount()));
        medicalOutpatientFee.setCollectionTime(sBMZFeeRecordsVoResponse.getCollectionTime());
        medicalOutpatientFee.setCollectionCompany(sBMZFeeRecordsVoResponse.getCollectionCompany());
        medicalOutpatientFee.setCollector(sBMZFeeRecordsVoResponse.getCollector());
        medicalOutpatientFee.setSelfCareAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getSelfCareAmount()));
        medicalOutpatientFee.setOverAllAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getOverallAmount()));
        medicalOutpatientFee.setInvoiceType(convertStringToShort(sBMZFeeRecordsVoResponse.getInvoiceType()));
        medicalOutpatientFee.setPayNo(sBMZFeeRecordsVoResponse.getPayFeeId());
        medicalOutpatientFee.setOrgCode(HOSPITAL_CODE);
        medicalOutpatientFee.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalOutpatientFee;
    }

    private MedicalChecking buildMedicalChecking(SBInspectRecordVoResponse sBInspectRecordVoResponse) {
        MedicalChecking medicalChecking = new MedicalChecking();
        medicalChecking.setId(PKGenerator.generateId());
        medicalChecking.setBusinessFeeId(sBInspectRecordVoResponse.getBusinessFeeId());
        medicalChecking.setBusinessType(convertStringToShort(sBInspectRecordVoResponse.getBusinessType()));
        medicalChecking.setInspectId(sBInspectRecordVoResponse.getInspectId());
        medicalChecking.setMzBillId(sBInspectRecordVoResponse.getMzBillId());
        medicalChecking.setAdmissionNo(sBInspectRecordVoResponse.getAdmissionNo());
        medicalChecking.setPatId(sBInspectRecordVoResponse.getPatId());
        medicalChecking.setSendHospitalCode(sBInspectRecordVoResponse.getSendHospitalCode());
        medicalChecking.setInspectTime(sBInspectRecordVoResponse.getInspectTime());
        medicalChecking.setSendDeptCode(sBInspectRecordVoResponse.getSendDeptCode());
        medicalChecking.setSendDeptName(sBInspectRecordVoResponse.getSendDeptName());
        medicalChecking.setSendDoctorCode(sBInspectRecordVoResponse.getSendDoctorCode());
        medicalChecking.setSendDoctorName(sBInspectRecordVoResponse.getSendDoctorName());
        medicalChecking.setInspectType(convertStringToShort(sBInspectRecordVoResponse.getInspectType()));
        medicalChecking.setInspectDeptCode(sBInspectRecordVoResponse.getInspectDeptCode());
        medicalChecking.setInspectDeptName(sBInspectRecordVoResponse.getSendDeptName());
        medicalChecking.setExecDoctorCode(sBInspectRecordVoResponse.getExecDoctorCode());
        medicalChecking.setExecDoctorName(sBInspectRecordVoResponse.getExecDoctorName());
        medicalChecking.setAuditDoctorCode(sBInspectRecordVoResponse.getAuditDoctorCode());
        medicalChecking.setAuditDoctorName(sBInspectRecordVoResponse.getAuditDoctorName());
        medicalChecking.setResultDesc(sBInspectRecordVoResponse.getResultDesc());
        medicalChecking.setCreatedTime(DateUtils.dateToString(new Date()));
        medicalChecking.setOrgCode(HOSPITAL_CODE);
        medicalChecking.setUploadFlag(new Short("0"));
        medicalChecking.setUploadTimes(0);
        return medicalChecking;
    }

    private MedicalCheckingDetail buildMedicalCheckingDetail(SBInspectDetail sBInspectDetail) {
        MedicalCheckingDetail medicalCheckingDetail = new MedicalCheckingDetail();
        medicalCheckingDetail.setId(PKGenerator.generateId());
        medicalCheckingDetail.setItemCode(sBInspectDetail.getItemCode());
        medicalCheckingDetail.setItemName(sBInspectDetail.getItemName());
        medicalCheckingDetail.setInspectMethod(sBInspectDetail.getInspectMethod());
        medicalCheckingDetail.setInspectDesc(sBInspectDetail.getInspectDesc());
        medicalCheckingDetail.setSampleType(sBInspectDetail.getSampleType());
        medicalCheckingDetail.setSampleCollectPart(sBInspectDetail.getSampleCollectPart());
        medicalCheckingDetail.setInspectResult(sBInspectDetail.getInspectResult());
        medicalCheckingDetail.setInspectDetailId(sBInspectDetail.getInspectDetailId());
        medicalCheckingDetail.setAuditTime(sBInspectDetail.getAuditTime());
        medicalCheckingDetail.setReportDoctorCode(sBInspectDetail.getReportDoctorCode());
        medicalCheckingDetail.setReportDoctorName(sBInspectDetail.getReportDoctorName());
        medicalCheckingDetail.setReportTime(sBInspectDetail.getReportTime());
        medicalCheckingDetail.setInspectResultCode(sBInspectDetail.getInspectResultCode());
        medicalCheckingDetail.setResultValue(sBInspectDetail.getResultValue());
        medicalCheckingDetail.setUnit(sBInspectDetail.getUnit());
        medicalCheckingDetail.setRefRange(sBInspectDetail.getRefRange());
        medicalCheckingDetail.setCreatedTime(DateUtils.dateToString(new Date()));
        medicalCheckingDetail.setOrgCode(HOSPITAL_CODE);
        medicalCheckingDetail.setUploadFlag(new Short("0"));
        medicalCheckingDetail.setUploadTimes(0);
        return medicalCheckingDetail;
    }

    private MedicalExamination buildExamination(SBCheckRecordVoResponse sbCheckRecordVoResponse) {
        MedicalExamination medicalExamination = new MedicalExamination();
        medicalExamination.setId(PKGenerator.generateId());
        medicalExamination.setBusinessFeeId(sbCheckRecordVoResponse.getBusinessFeeId());
        medicalExamination.setBusinessType(convertStringToShort(sbCheckRecordVoResponse.getBusinessType()));
        medicalExamination.setCheckId(sbCheckRecordVoResponse.getCheckId());
        medicalExamination.setMzBillId(sbCheckRecordVoResponse.getMzBillId());
        medicalExamination.setAdmissionNo(sbCheckRecordVoResponse.getAdmissionNo());
        medicalExamination.setPatId(sbCheckRecordVoResponse.getPatId());
        medicalExamination.setSendDeptName(sbCheckRecordVoResponse.getSendDeptName());
        medicalExamination.setSendDoctorName(sbCheckRecordVoResponse.getSendDoctorName());
        medicalExamination.setSendDoctorName(sbCheckRecordVoResponse.getSendDoctorName());
        medicalExamination.setEmergencyCheckFlag(convertStringToShort(sbCheckRecordVoResponse.getEmergencyCheckFlag()));
        medicalExamination.setCheckDeptCode(sbCheckRecordVoResponse.getCheckDeptCode());
        medicalExamination.setCheckDeptName(sbCheckRecordVoResponse.getCheckDeptName());
        medicalExamination.setExecDoctorCode(sbCheckRecordVoResponse.getExecDoctorCode());
        medicalExamination.setExecDoctorName(sbCheckRecordVoResponse.getExecDoctorName());
        medicalExamination.setAuditDoctorName(sbCheckRecordVoResponse.getAuditDoctorName());
        medicalExamination.setAuditTime(sbCheckRecordVoResponse.getAuditTime());
        medicalExamination.setCheckType(sbCheckRecordVoResponse.getCheckType());
        medicalExamination.setCheckTime(sbCheckRecordVoResponse.getCheckTime());
        medicalExamination.setCheckMethod(sbCheckRecordVoResponse.getCheckMethod());
        medicalExamination.setCheckDesc(sbCheckRecordVoResponse.getCheckDesc());
        medicalExamination.setItemCode(sbCheckRecordVoResponse.getItemCode());
        medicalExamination.setChineseName(sbCheckRecordVoResponse.getChineseName());
        medicalExamination.setEnglishName(sbCheckRecordVoResponse.getEnglishName());
        medicalExamination.setCheckResultCode(sbCheckRecordVoResponse.getCheckResultCode());
        medicalExamination.setCheckResultDesc(sbCheckRecordVoResponse.getCheckResultDesc());
        medicalExamination.setImagingDesc(sbCheckRecordVoResponse.getImagingDesc());
        medicalExamination.setImagingDiagnosis(sbCheckRecordVoResponse.getImagingDiagnosis());
        medicalExamination.setReportDoctorCode(sbCheckRecordVoResponse.getReportDoctorCode());
        medicalExamination.setReportDoctorName(sbCheckRecordVoResponse.getReportDoctorName());
        medicalExamination.setReportTime(sbCheckRecordVoResponse.getReportTime());
        medicalExamination.setRisrelationNo(sbCheckRecordVoResponse.getRisRelationNo());
        medicalExamination.setImagingId(sbCheckRecordVoResponse.getImagingId());
        medicalExamination.setDeviceType(sbCheckRecordVoResponse.getDeviceType());
        medicalExamination.setClinicalDiagnosis(sbCheckRecordVoResponse.getClinicalDiagnosis());
        medicalExamination.setCheckPartCode(sbCheckRecordVoResponse.getCheckPartCode());
        medicalExamination.setCheckPart(sbCheckRecordVoResponse.getCheckPart());
        medicalExamination.setPictureNo(sbCheckRecordVoResponse.getPictureNo());
        medicalExamination.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalExamination;
    }


    private Integer conveterStringToInt(String value) {
        return value == null ? null : Integer.valueOf(value);
    }

    private Short convertStringToShort(String value) {
        return value == null ? null : Short.valueOf(value);
    }


}
